package com.vegetable.modules.controller.finance;

import com.vegetable.common.utils.R;
import com.vegetable.modules.config.ChinaUMSConfig;
import com.vegetable.modules.finance.service.ChinaUMSProxyService;
import com.vegetable.modules.finance.entity.proxy.SinglePaymentRequest;
import com.vegetable.modules.finance.entity.proxy.DecryptAndVerifyRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 银联商务代付业务代理控制器
 * 提供银联商务代付业务接口的HTTP访问入口
 */
@Slf4j
@RestController
@RequestMapping("/chinaums")
@Api(tags = "银联商务代付业务接口")
public class ChinaUMSProxyController {
    
    @Autowired
    private ChinaUMSProxyService chinaUMSProxyService;
    
    @Autowired
    private ChinaUMSConfig chinaUMSConfig;
    
    /**
     * 单笔代付接口
     * @param request 业务参数
     * @param isProduction 是否生产环境，默认根据配置确定
     * @return 响应结果
     */
    @PostMapping("/singlePayment")
    @ApiOperation("单笔代付接口")
    public R singlePayment(@RequestBody SinglePaymentRequest request, 
                          @RequestParam(defaultValue = "${chinaums.production:false}") boolean isProduction) {
        try {
            // 构建参数Map
            Map<String, String> params = new HashMap<>();
            params.put("sysOrderId", request.getSysOrderId());
            params.put("mchntNo", request.getMchntNo());
            params.put("responseUrl", request.getResponseUrl());
            params.put("revAcctName", request.getRevAcctName());
            params.put("revIdCardNum", request.getRevIdCardNum());
            params.put("revAcctNo", request.getRevAcctNo());
            params.put("payAmt", request.getPayAmt());
            if (request.getRemark() != null) {
                params.put("remark", request.getRemark());
            }
            if (request.getVerify() != null) {
                params.put("verify", request.getVerify());
            }
            if (request.getMobilePhone() != null) {
                params.put("mobilePhone", request.getMobilePhone());
            }
            if (request.getTermNo() != null) {
                params.put("termNo", request.getTermNo());
            }
            if (request.getAssociateTransKey() != null) {
                params.put("associateTransKey", request.getAssociateTransKey());
            }
            if (request.getPyeeAcctTp() != null) {
                params.put("pyeeAcctTp", request.getPyeeAcctTp());
            }
            if (request.getPyerAcctTp() != null) {
                params.put("pyerAcctTp", request.getPyerAcctTp());
            }
            if (request.getPyeeBkId() != null) {
                params.put("pyeeBkId", request.getPyeeBkId());
            }
            if (request.getPyeeBkNo() != null) {
                params.put("pyeeBkNo", request.getPyeeBkNo());
            }
            
            Map<String, Object> result = chinaUMSProxyService.singlePayment(params, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("单笔代付接口调用失败", e);
            return R.fail("单笔代付接口调用失败：" + e.getMessage());
        }
    }
    
    /**
     * 单笔代付结果查询接口
     * @param sysOrderId 系统订单号
     * @param mchntNo 商户编号
     * @param trxDate 交易日期
     * @param isProduction 是否生产环境，默认根据配置确定
     * @return 响应结果
     */
    @GetMapping("/queryPaymentStatus")
    @ApiOperation("单笔代付结果查询接口")
    public R queryPaymentStatus(@RequestParam String sysOrderId,
                               @RequestParam String mchntNo,
                               @RequestParam String trxDate,
                               @RequestParam(defaultValue = "${chinaums.production:false}") boolean isProduction) {
        try {
            Map<String, Object> result = chinaUMSProxyService.queryPaymentStatus(sysOrderId, mchntNo, trxDate, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("单笔代付结果查询接口调用失败", e);
            return R.fail("单笔代付结果查询接口调用失败：" + e.getMessage());
        }
    }
    
    /**
     * 可代付余额查询接口
     * @param mchntNo 商户编号
     * @param isProduction 是否生产环境，默认根据配置确定
     * @return 响应结果
     */
    @GetMapping("/queryPaymentBalance")
    @ApiOperation("可代付余额查询接口")
    public R queryPaymentBalance(@RequestParam String mchntNo,
                                @RequestParam(defaultValue = "${chinaums.production:false}") boolean isProduction) {
        try {
            Map<String, Object> result = chinaUMSProxyService.queryPaymentBalance(mchntNo, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("可代付余额查询接口调用失败", e);
            return R.fail("可代付余额查询接口调用失败：" + e.getMessage());
        }
    }
    
    /**
     * 解密并验签响应结果
     * @param request 响应内容
     * @return 解密后的结果
     */
    @PostMapping("/decryptAndVerify")
    @ApiOperation("解密并验签响应结果")
    public R decryptAndVerify(@RequestBody DecryptAndVerifyRequest request) {
        try {
            String content = request.getContent();
            if (content == null || content.isEmpty()) {
                return R.fail("content参数不能为空");
            }
            Map<String, Object> result = chinaUMSProxyService.decryptAndVerify(content);
            return R.data(result);
        } catch (Exception e) {
            log.error("解密并验签失败", e);
            return R.fail("解密并验签失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取当前配置信息
     * @return 配置信息
     */
    @GetMapping("/config")
    @ApiOperation("获取当前配置信息")
    public R getConfig() {
        try {
            Map<String, Object> configInfo = new java.util.HashMap<>();
            configInfo.put("sysId", chinaUMSConfig.getSysId());
            configInfo.put("msgSrc", chinaUMSConfig.getMsgSrc());
            configInfo.put("production", chinaUMSConfig.isProduction());
            configInfo.put("testUrls", chinaUMSConfig.getTest());
            configInfo.put("productionUrls", chinaUMSConfig.getProductionConfig());
            return R.data(configInfo);
        } catch (Exception e) {
            log.error("获取配置信息失败", e);
            return R.fail("获取配置信息失败：" + e.getMessage());
        }
    }
}